package com.example.chirdrenserver.Service.AdminService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.dto.admin.AdminPostUpdateDTO;
import com.example.chirdrenserver.Pojo.entity.File;
import com.example.chirdrenserver.Pojo.entity.Post;
import com.example.chirdrenserver.Pojo.entity.User;
import com.example.chirdrenserver.Pojo.vo.PostVO;
import com.example.chirdrenserver.Pojo.vo.admin.AdminPostVO;
import com.example.chirdrenserver.Service.AdminService.AdminPostService;
import com.example.chirdrenserver.Service.AdminService.UserService;
import com.example.chirdrenserver.Service.FileService;
import com.example.chirdrenserver.exception.BaseException;
import com.example.chirdrenserver.mapper.PostMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author Yu
 * @Description //TODO $
 * @Date $ $
 **/
@Service
public class AdminPostServiceImpl extends ServiceImpl<PostMapper, Post> implements AdminPostService {
    @Autowired
    FileService fileService;
    @Autowired
    UserService userService;

    @Override
    public Collection<AdminPostVO> listPost(BasePage basePage) {
        List<AdminPostVO> adminPostVOList = new ArrayList<>();
        List<Post> postList = page(basePage.page()).getRecords();
        // final AtomicInteger count = new AtomicInteger(0);
        for (Post post : postList) {
            AdminPostVO adminPostVO = createAdminVO(post);
            BeanUtils.copyProperties(post, adminPostVO);
            adminPostVOList.add(adminPostVO);
        }
        return adminPostVOList;
    }

    @Override
    public AdminPostVO getInfoById(Integer id) {
        AdminPostVO adminPostVO = createAdminVO(getById(id));
        return adminPostVO;
    }

    private AdminPostVO createAdminVO(Post post) {
        AdminPostVO adminPostVO = new AdminPostVO();
        File file = fileService.getById(post.getFileId());
        User user = userService.getById(post.getUserId());
        adminPostVO.setFileKey(file.getFileKey());
        adminPostVO.setFileId(file.getFileId());
        adminPostVO.setUserId(user.getId());
        adminPostVO.setNickName(user.getNickname());
        return adminPostVO;
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        AdminPostVO infoById = getInfoById(id);
        if (!infoById.getPostType().equals("conetext")) {

            if(!fileService.removeById(infoById.getFileId()));
            {
                throw new BaseException("文件删除异常");
            }
        }
        if(!removeById(id))
        {
            throw new BaseException("删除失败失败");
        };
    }

    @Override
    public void updatePost(AdminPostUpdateDTO postUpdateDTO) {
        AdminPostVO infoById=getInfoById(postUpdateDTO.getPostId());
        Post post = new Post();
        BeanUtils.copyProperties(infoById,post);
        if(updateById(post))
        {
            throw new BaseException("更新失败");
        }

    }

    @Override
    public Collection<AdminPostVO> getPostLike(String postTit) {
        List<AdminPostVO> adminPostVOList = new ArrayList<>();
        List<Post> postList = list(new LambdaQueryWrapper<Post>().like(Post::getTitle,postTit));
        for (Post post : postList) {
            AdminPostVO adminPostVO = createAdminVO(post);
            BeanUtils.copyProperties(adminPostVOList, post);
            adminPostVOList.add(adminPostVO);
        }
        return adminPostVOList;
    }
}
